Overview
A LiveView is a process that receives events, updates its state, and renders updates to a page as diffs. LiveView begins as a regular HTTP request and HTML response, and then upgrades to a stateful view on client connect, guaranteeing a regular HTML page even if JavaScript is disabled. Socket assigns are stateful values kept on the server side.Life-cycle
The LiveView life-cycle follows this sequence:mount/3- Invoked on both initial HTTP render and WebSocket connectionhandle_params/3- Invoked after mount and on live patch eventsrender/1- Renders the template whenever state changes
Callbacks
mount/3
A map of string keys containing query params and router path parameters. Returns
:not_mounted_at_router if not mounted at router.The connection session containing private data set by the application.
The LiveView socket.
Returns updated socket with optional configuration:
:temporary_assigns- Assigns reset after each render:layout- Optional layout override
render/1
The socket assigns map.
Must return a template defined via the
~H sigil.handle_params/3
Current parameters including router params and query params.
The current URI from the client.
The LiveView socket.
handle_event/3
The event name (e.g., “save”, “delete”).
The event payload as a map.
The LiveView socket.
Returns updated socket, optionally with a reply map sent back to client.
handle_info/2
The message received from another process.
The LiveView socket.
handle_async/3
start_async/3 operation is available.
The name given to
start_async/3.The async function result wrapped in
:ok or :exit.The LiveView socket.
handle_call/3
call messages from other Elixir processes via GenServer.call/2.
The message sent via
GenServer.call/2.The caller’s PID and reply reference.
The LiveView socket.
Either
:noreply for no response or :reply with a return value for the caller.handle_cast/2
GenServer.cast/2.
The message sent via
GenServer.cast/2.The LiveView socket.
Returns updated socket.
terminate/2
The termination reason:
:normal- Normal termination:shutdown- Application shutdown{:shutdown, :left}- User navigated away{:shutdown, :closed}- Connection closed
The LiveView socket.
Return value is ignored.
Functions
assign/3
Adds a key-value pair to socket assigns.The LiveView socket.
The assign key.
The assign value.
Updated socket with new assign.
assign_new/3
Assigns a key with value from function if one does not yet exist.The LiveView socket.
The assign key.
Function returning the value (0 or 1 arity).
update/3
Updates an existing key with a function.The LiveView socket.
The assign key to update.
Function receiving current value and returning updated value.
push_event/3
Pushes an event to the client.The LiveView socket.
The event name.
The event data.
Options:
:dispatch-:beforeor:afterpatch (default::after)
push_navigate/2
Annotates the socket for navigation to another LiveView in the samelive_session.
The LiveView socket.
Options:
:to(required) - The path to navigate to:replace- Replace browser history (default:false)
push_patch/2
Annotates the socket for navigation within the current LiveView.The LiveView socket.
Options:
:to(required) - The path to patch to:replace- Replace browser history (default:false)
redirect/2
Annotates the socket for redirect to a destination path.The LiveView socket.
Options:
:to- Local path to redirect to:external- External URL to redirect to:status- HTTP status code (default: 302)
put_flash/3
Adds a flash message to the socket.The LiveView socket.
The flash type (
:info, :error, etc.).The flash message.
stream/4
Assigns a new stream or inserts items into an existing stream.The LiveView socket.
The stream name.
Items to insert.
Options:
:at- Index to insert items (default:-1):reset- Reset stream on client (default:false):limit- Limit items in UI
stream_insert/4
Inserts a new item or updates an existing item in the stream.The LiveView socket.
The stream name.
Item to insert or update.
Options:
:at- Index to insert item (default:-1):limit- Limit items in UI
stream_delete/3
Deletes an item from the stream.The LiveView socket.
The stream name.
Item to delete.
stream_delete_by_dom_id/3
Deletes a stream item by its DOM ID.The LiveView socket.
The stream name.
The DOM ID of the item to delete.
stream_configure/3
Configures a stream with options.The LiveView socket.
The stream name.
Options:
:dom_id- Function to generate DOM IDs
assign_async/3
Assigns keys asynchronously using a task.The LiveView socket.
Key or list of keys to assign.
Function returning
{:ok, assigns} or {:error, reason}.Options:
:supervisor-Task.Supervisorto use:reset- Remove previous results (default:false)
start_async/3
Wraps a function in an asynchronous task.The LiveView socket.
Name for the async operation.
Function to execute asynchronously.
Options:
:supervisor-Task.Supervisorto use
send_update/3
Asynchronously updates a LiveComponent with new assigns.The LiveView process ID.
The LiveComponent module.
Assigns to update (must include
:id).cancel_async/3
Cancels one or more async operations.The LiveView socket.
Async key(s) to cancel or an AsyncResult struct.
The cancellation reason.
attach_hook/4
Attaches a function by name for a lifecycle stage.The LiveView socket.
Hook name.
Lifecycle stage:
:handle_params, :handle_event, :handle_info, :handle_async, :after_render.Hook function.
connected?/1
Returns true if the socket is connected.The LiveView socket.
true if connected via WebSocket, false during initial HTTP render.get_connect_params/1
Accesses the connect params sent by the client.The LiveView socket.
Connect params on connected mount,
nil when disconnected.get_connect_info/2
Gets connection metadata by key.The LiveView socket.
Connection info key (
:peer_data, :x_headers, :uri, :user_agent, etc.).Connection metadata value or
nil.static_changed?/1
Checks if static assets have changed since the LiveView was rendered.The LiveView socket.
true if static assets changed during the connection.clear_flash/1
Clears all flash messages.The LiveView socket.
clear_flash/2
Clears a specific flash message key.The LiveView socket.
Flash key to clear.
detach_hook/3
Detaches a hook by ID and lifecycle stage.The LiveView socket.
Hook identifier.
Lifecycle stage (
:mount, :handle_params, :handle_event, :handle_info, :after_render).allow_upload/3
Allows an upload for the provided name.The LiveView socket.
Upload name.
Options:
:accept(required) - File extensions list or:any:max_entries- Maximum files (default: 1):max_file_size- Maximum bytes (default: 8MB):chunk_size- Chunk size in bytes (default: 64KB):auto_upload- Auto-upload on selection (default:false):external- Function for external uploads:progress- Progress callback function